import vue from 'vue';

/* 时间戳转化时间 */

function zerofill(num) {
  // 补零
  return num < 10 ? '0' + num : num;
}

//  时间: 12:12:50
vue.filter('time', function (timeStamp) {
  timeStamp = Number(timeStamp);
  let now = new Date(timeStamp);
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  return zerofill(hour) + ":" + zerofill(minute) +":" + zerofill(second);
});

// 日期: 2022-12-11
vue.filter('dateFormat', function (timeStamp) {
  timeStamp = Number(timeStamp);
  let now = new Date(timeStamp);
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  return year + '-' + zerofill(month) + '-' + zerofill(date);

});

// 日期-时间: 2022-12-11 12:12:50
vue.filter('date',  function (timeStamp) {
  timeStamp = Number(timeStamp);
  let now = new Date(timeStamp);
  let year = now.getFullYear();
  let month = now.getMonth() + 1;
  let date = now.getDate();
  let hour = now.getHours();
  let minute = now.getMinutes();
  let second = now.getSeconds();
  return year + '-' + zerofill(month) + '-' + zerofill(date) + ' ' + zerofill(hour) + ":" + zerofill(minute) +":" + zerofill(second);

 });

vue.filter('dateUTC', function (timeStamp) {
  timeStamp = Number(timeStamp);
  const date = new Date(timeStamp);
  const year = date.getUTCFullYear();
  const month = String(date.getUTCMonth() + 1).padStart(2, '0');
  const day = String(date.getUTCDate()).padStart(2, '0');
  const hours = String(date.getUTCHours()).padStart(2, '0');
  const minutes = String(date.getUTCMinutes()).padStart(2, '0');
  const seconds = String(date.getUTCSeconds()).padStart(2, '0');
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
})